package com.lh.web.ctrl;

import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lh.ctrl.BaseCtrl;
import com.lh.entity.User;
import com.lh.util.SessionUtil;
import com.lh.util.ValidateCodeImgUtil;

@Controller
public class CommonCtrl extends BaseCtrl {
	public static final String VALID_CODE_NAME = "validCode";

	@RequestMapping("valid-code-img")
	public void validCodeImg(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 设置页面不缓存
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);

		OutputStream os = response.getOutputStream();
		String verificationCode = ValidateCodeImgUtil.generate(os);
		os.flush();
		os.close();
		request.getSession().setAttribute(VALID_CODE_NAME, verificationCode);
	}

	@ResponseBody
	@RequestMapping("current-user")
	public User obtainCurrentUser() {
		User u =  SessionUtil.getUser();
		return u;
	}

}
